import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/index.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: { title: '首页', icon: 'House' }
      }
    ]
  },
  {
    path: '/customer',
    component: Layout,
    redirect: '/customer/clue',
    name: 'Customer',
    meta: { title: '客户管理', icon: 'User' },
    children: [
      {
        path: 'clue',
        name: 'ClueCustomer',
        component: () => import('@/views/customer/clue.vue'),
        meta: { title: '线索客户', icon: 'User' }
      },
      {
        path: 'public',
        name: 'PublicCustomer',
        component: () => import('@/views/customer/public.vue'),
        meta: { title: '公海客户', icon: 'User' }
      },
      {
        path: 'deal',
        name: 'DealCustomer',
        component: () => import('@/views/customer/deal.vue'),
        meta: { title: '成交客户', icon: 'User' }
      },
      {
        path: 'opportunity',
        name: 'CustomerOpportunity',
        component: () => import('@/views/customer/opportunity.vue'),
        meta: { title: '客户机会', icon: 'Star' }
      },
      {
        path: 'contact',
        name: 'CustomerContact',
        component: () => import('@/views/customer/contact.vue'),
        meta: { title: '客户联系人', icon: 'Phone' }
      },
      {
        path: 'follow',
        name: 'FollowRecord',
        component: () => import('@/views/customer/follow.vue'),
        meta: { title: '跟进记录', icon: 'ChatDotRound' }
      }
    ]
  },
  {
    path: '/contract',
    component: Layout,
    redirect: '/contract/list',
    name: 'Contract',
    meta: { title: '合同管理', icon: 'Document' },
    children: [
      {
        path: 'list',
        name: 'ContractList',
        component: () => import('@/views/contract/index.vue'),
        meta: { title: '合同列表', icon: 'Document' }
      },
      {
        path: 'payment',
        name: 'Payment',
        component: () => import('@/views/contract/payment.vue'),
        meta: { title: '回款管理', icon: 'Money' }
      },
      {
        path: 'adjust',
        name: 'ContractAdjust',
        component: () => import('@/views/contract/adjust.vue'),
        meta: { title: '合同额调整', icon: 'Edit' }
      }
    ]
  },
  {
    path: '/invoice',
    component: Layout,
    redirect: '/invoice/list',
    name: 'Invoice',
    meta: { title: '开票管理', icon: 'Ticket' },
    children: [
      {
        path: 'list',
        name: 'InvoiceList',
        component: () => import('@/views/invoice/index.vue'),
        meta: { title: '开票申请', icon: 'Ticket' }
      },
      {
        path: 'record',
        name: 'InvoiceRecord',
        component: () => import('@/views/invoice/record.vue'),
        meta: { title: '开票记录', icon: 'DocumentChecked' }
      }
    ]
  },
  {
    path: '/approval',
    component: Layout,
    redirect: '/approval/task',
    name: 'Approval',
    meta: { title: '审批管理', icon: 'Checked' },
    children: [
      {
        path: 'task',
        name: 'ApprovalTask',
        component: () => import('@/views/approval/task.vue'),
        meta: { title: '审批任务', icon: 'Checked' }
      },
      {
        path: 'flow',
        name: 'ApprovalFlow',
        component: () => import('@/views/approval/flow.vue'),
        meta: { title: '审批流程', icon: 'Connection' }
      }
    ]
  },
  {
    path: '/system',
    component: Layout,
    redirect: '/system/user',
    name: 'System',
    meta: { title: '系统管理', icon: 'Setting' },
    children: [
      {
        path: 'user',
        name: 'SystemUser',
        component: () => import('@/views/system/user.vue'),
        meta: { title: '用户管理', icon: 'UserFilled' }
      },
      {
        path: 'role',
        name: 'SystemRole',
        component: () => import('@/views/system/role.vue'),
        meta: { title: '角色管理', icon: 'Avatar' }
      },
      {
        path: 'menu',
        name: 'SystemMenu',
        component: () => import('@/views/system/menu.vue'),
        meta: { title: '菜单管理', icon: 'Menu' }
      },
      {
        path: 'dept',
        name: 'SystemDept',
        component: () => import('@/views/system/dept.vue'),
        meta: { title: '部门管理', icon: 'OfficeBuilding' }
      },
      {
        path: 'dict',
        name: 'SystemDict',
        component: () => import('@/views/system/dict.vue'),
        meta: { title: '字典管理', icon: 'Collection' }
      },
      {
        path: 'log',
        name: 'SystemLog',
        component: () => import('@/views/system/log.vue'),
        meta: { title: '操作日志', icon: 'Document' }
      }
    ]
  },
  {
    path: '/analysis',
    component: Layout,
    redirect: '/analysis/brief',
    name: 'Analysis',
    meta: { title: '数据分析', icon: 'DataAnalysis' },
    children: [
      {
        path: 'brief',
        name: 'AnalysisBrief',
        component: () => import('@/views/analysis/brief.vue'),
        meta: { title: '简报看板', icon: 'DataBoard' }
      },
      {
        path: 'target',
        name: 'AnalysisTarget',
        component: () => import('@/views/analysis/target.vue'),
        meta: { title: '业绩目标', icon: 'TrendCharts' }
      },
      {
        path: 'customer',
        name: 'AnalysisCustomer',
        component: () => import('@/views/analysis/customer.vue'),
        meta: { title: '客户统计', icon: 'PieChart' }
      },
      {
        path: 'performance',
        name: 'AnalysisPerformance',
        component: () => import('@/views/analysis/performance.vue'),
        meta: { title: '业绩排行', icon: 'Trophy' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (!token && to.path !== '/login') {
    next('/login')
  } else {
    next()
  }
})

export default router
